package com.ride.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.ride.dao.UserDao;
import com.ride.model.Operator;
import org.springframework.stereotype.Repository;

/**
 * {@inheritDoc}
 *
 * @version $LastChangedRevision: 16 $
 *          <p/>
 *          $LastChangedBy: marcel.menze@gmail.com $
 *          <p/>
 *          $LastChangedDate: 2011-04-05 12:34:26 +0000 (Tue, 05 Apr 2011) $
 */
@Repository
public class UserDaoImpl implements UserDao {

    @PersistenceContext(name = "ride")
    private EntityManager em;

    public UserDaoImpl() {
    }

    @Override
    public void persist(Operator operator) {
        em.persist(operator);
    }

    public Operator findById(Long id) {
        return em.find(Operator.class, id);
    }

    @Override
    public void update(Operator operator) {
        em.merge(operator);
    }

    @Override
    public void delete(Operator operator) {
        em.remove(operator);
    }

    /**
     * {@inheritDoc}
     */
    public Operator findByUsername(String username) {
        return (Operator) em.createQuery("select u from Operator u where u.username  =:username ").
                setParameter("username", username).getSingleResult();
    }

    /**
     * {@inheritDoc}
     */
    public List<Operator> findAllUsers() {
        List<Operator> operators = em.createQuery("select u from Operator u").getResultList();
        return operators;
    }

}
